//
//  PickImageSourceAlertView.m
//  ZooNgonDoiOS
//
//  Created by Tran Minh Long on 23/8/12.
//  Copyright (c) 2012 Personal. All rights reserved.
//

#import "PickImageSourceAlertView.h"
#import <QuartzCore/QuartzCore.h>

@implementation PickImageSourceAlertView
@synthesize pickSourceDelegate;

bool isRemoveSubview;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        isRemoveSubview = NO;
    }
    
    return self;
}

- (void)layoutSubviews {
    self.delegate = self;

    if (!isRemoveSubview) {
        for (UIView *subview in self.subviews) {
//            if ([subview isMemberOfClass:[UIImageView class]]) {
                subview.hidden = YES;
//            }
        }
        
        isRemoveSubview = YES;
    }
    
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        [alertView dismissWithClickedButtonIndex:0 animated:YES];
    }
}

- (void)willPresentAlertView:(UIAlertView *)alertView {

    alertView.frame = CGRectMake(20, 130, 280, 180);
    [alertView.layer setBorderWidth:1.5f];
    [alertView.layer setBorderColor:[UIColor grayColor].CGColor];
    
    // Title
    UIView *_vTitle = [[UIView alloc] initWithFrame:CGRectMake(0, 0, alertView.frame.size.width, 35)];
    
    UILabel *_lbalertTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, alertView.frame.size.width, 35)];
    [_lbalertTitle setText:@"Chọn ảnh"];
    [_lbalertTitle setBackgroundColor:[UIColor clearColor]];
    [_lbalertTitle setTextColor:[UIColor colorWithRed:49/255.0 green:129/255.0 blue:147/255.0 alpha:1]];

    CALayer *bottomBorder = [CALayer layer];
    bottomBorder.frame = CGRectMake(0, _vTitle.frame.size.height - 1, _vTitle.frame.size.width, 1.5f);
    bottomBorder.backgroundColor = [UIColor colorWithRed:46/255.0 green:129/255.0 blue:147/255.0 alpha:1].CGColor;
    
    [_vTitle addSubview:_lbalertTitle];
    [_vTitle.layer addSublayer:bottomBorder];
    
    [_lbalertTitle release];
    
    // Camera button
    _btnCameraSource = [[UIButton alloc] initWithFrame:CGRectMake(0, self.bounds.size.height / 10, self.bounds.size.width / 2, self.bounds.size.height * 2/3)];
    [_btnCameraSource setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
    [_btnCameraSource setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_btnCameraSource setImage:[UIImage imageNamed:IMG_CAMERA_PICKSOURCE_NORMAL] forState:UIControlStateNormal];
    [_btnCameraSource setImage:[UIImage imageNamed:IMG_CAMERA_PICKSOURCE_SELECTED] forState:UIControlStateHighlighted];
    [_btnCameraSource addTarget:self action:@selector(cameraClicked) forControlEvents:UIControlEventTouchUpInside];
    
    // Camera button's title
    UILabel *_lbCameraTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, _btnCameraSource.frame.size.height - 40, _btnCameraSource.frame.size.width, 20)];
    [_lbCameraTitle setText:@"Chụp ảnh"];
    [_lbCameraTitle setBackgroundColor:[UIColor clearColor]];
    [_lbCameraTitle setTextAlignment:UITextAlignmentCenter];
    
    [_btnCameraSource addSubview:_lbCameraTitle];
    [_lbCameraTitle release];
    
    // Gallery button
    _btnGallerySource = [[UIButton alloc] initWithFrame:CGRectMake(self.bounds.origin.x + self.bounds.size.width / 2, self.bounds.size.height / 10, self.bounds.size.width / 2, self.bounds.size.height * 2/3)];
    [_btnGallerySource setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
    [_btnGallerySource setImage:[UIImage imageNamed:IMG_GALLERY_PICKSOURCE_NORMAL] forState:UIControlStateNormal];
    [_btnGallerySource addTarget:self action:@selector(galleryClicked) forControlEvents:UIControlEventTouchUpInside];
    
    // Gallery button's title
    UILabel *_lbGalleryTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, _btnGallerySource.frame.size.height - 40, _btnGallerySource.frame.size.width, 20)];
    [_lbGalleryTitle setText:@"Chọn ảnh từ máy"];
    [_lbGalleryTitle setBackgroundColor:[UIColor clearColor]];
    [_lbGalleryTitle setTextAlignment:UITextAlignmentCenter];
    
    [_btnGallerySource addSubview:_lbGalleryTitle];
    [_lbGalleryTitle release];
    
    // Cancel button
    _btnCancel = [[UIButton alloc] initWithFrame:CGRectMake(20, self.bounds.size.height - 50, self.bounds.size.width - 40, 40)];
    [_btnCancel setTitle:@"Cancel" forState:UIControlStateNormal];
    [_btnCancel setBackgroundColor:[UIColor colorWithRed:163/255.0 green:18/255.0 blue:25/255.0 alpha:1]];
    [_btnCancel addTarget:self action:@selector(cancelClicked) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:_vTitle];
    [self addSubview:_btnCameraSource];
    [self addSubview:_btnGallerySource];
    [self addSubview:_btnCancel];
    
    [_vTitle release];
    [_btnCameraSource release];
    [_btnGallerySource release];
    [_btnCancel release];
}

// Events handler

- (void) cameraClicked {
    [pickSourceDelegate pickCameraSource];
    [self dismissWithClickedButtonIndex:0 animated:YES];
}

- (void) galleryClicked {
    [pickSourceDelegate pickGallerySource];
    [self dismissWithClickedButtonIndex:0 animated:YES];
}

- (void) cancelClicked {
    [self dismissWithClickedButtonIndex:0 animated:YES];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    UIImage *backgroundImage = [UIImage imageNamed:IMG_VIEW_BACKGROUND];

    [backgroundImage drawInRect:self.bounds];
}
@end
